import Vue from 'vue';
import Vuex from 'vuex';

Vue.use(Vuex);
import LoginStore from './modules/login';
import ConversationStore from './modules/conversation';
import MessageStore from './modules/message';
import createPersistedState from "vuex-persistedstate"
const UserKey = 'KEY_USER_INFO';
const TokenKey = "KEY_TOKEN"


const store = new Vuex.Store({
	modules: {
	  LoginStore,
	  ConversationStore,
	  MessageStore
	},
	plugins: [createPersistedState({
	        storage: {
	            getItem: key => uni.getStorageSync(key),
	            setItem: (key, value) => uni.setStorageSync(key, value),
	            removeItem: key => uni.removeStorageSync(key)
	        }
	    })],
	state: {
		// locale: uni.getLocale(),
		language: uni.getStorageSync('language') || 'en',
		dir: uni.getLocale() == 'ar' ? 'rtl' : 'ltl',
		page_font_size: uni.getStorageSync('page_font_size') || 'page_font_size',
		StatusBar: {
			statusBar: 0,
			customBar: 0,
		},
		dict: {
			saleType: [],
			houseType: [],
			priceUnit: [],
			pricePeriod: [],
			cityList: [],
			systemConfig: {},
		},
		user: uni.getStorageSync(UserKey) || null
	},
	getters: {
		saleType(state) {
			return state.dict.saleType
		},
		houseType(state) {
			console.log('houseType',state.dict.houseType)
			return state.dict.houseType
		},
		priceUnit(state) {
			return state.dict.priceUnit
		},
		pricePeriod(state) {
			return state.dict.pricePeriod
		},
		systemConfig(state) {
			return state.dict.systemConfig
		},
		cityList(state) {
			return state.dict.cityList
		},
	},
	mutations: {
		RESET_STORE: (state) => {
		  console.log('>>>>>重置store', store);
		  store.commit('RESET_MESSAGE_STORE');
		  store.commit('RESET_LOGIN_STORE');
		},
		// 设置导航栏高度
		SET_STATUSBAR(state, value) {
			state.StatusBar = value
		},
		SET_page_font_size(state, value) {
			state.page_font_size = value
			uni.setStorage({
				key: 'page_font_size',
				data: value,
				success: function() {}
			});
		},
		updateUser(state, user) {
			state.user = user
		},
		removeUser(state) {
			state.user = null
		},
		updateDict(state, user) {
			state.dict = user;
		},
		SET_LANGUAGE(state, language) {
			state.language = language;
			uni.setStorageSync('language',language)
			// localStorage.setItem('language', language);
		}
	},
	actions: {
		initDict({
			commit,
			state
		}, dict) {
			commit('updateDict', dict)
		},
		setUser({
			commit,
			state
		}, user) {
			uni.setStorageSync(UserKey, user)
			commit('updateUser', user)
		},
		updateUser({
			commit,
			state
		}, user) {
			var data = {
				...state.user,
				...user
			}
			commit('updateUser', data)
		},
		removeUser({
			commit,
			state
		}) {
			uni.removeStorageSync(UserKey)
			commit('removeUser')
		},
		setLanguage({commit}, language) {
			commit('SET_LANGUAGE', language);
		}
	}
})
export default store